package com.example.aijiancece.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.aijiancece.LoginActivity;
import com.example.aijiancece.R;
import com.example.aijiancece.UserManager;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import androidx.appcompat.app.AppCompatDelegate;
import com.example.aijiancece.ThemeManager;
import com.example.aijiancece.UserInfoActivity;

public class SettingsFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_settings, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        View layoutUserInfo = view.findViewById(R.id.layoutUserInfo);
        View layoutTheme = view.findViewById(R.id.layoutTheme);
        MaterialButton btnLogout = view.findViewById(R.id.btnLogout);

        layoutUserInfo.setOnClickListener(v -> {
            startActivity(new Intent(requireContext(), UserInfoActivity.class));
            requireActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        });

        layoutTheme.setOnClickListener(v -> showThemeDialog());

        btnLogout.setOnClickListener(v -> showLogoutDialog());
    }

    private void showThemeDialog() {
        String[] themes = {"浅色", "深色", "跟随系统"};
        int currentTheme = ThemeManager.getInstance(requireContext()).getThemeMode();
        int selectedIndex = 2; // 默认选中"跟随系统"
        
        switch (currentTheme) {
            case AppCompatDelegate.MODE_NIGHT_NO:
                selectedIndex = 0;
                break;
            case AppCompatDelegate.MODE_NIGHT_YES:
                selectedIndex = 1;
                break;
        }

        new MaterialAlertDialogBuilder(requireContext())
                .setTitle("选择主题")
                .setSingleChoiceItems(themes, selectedIndex, (dialog, which) -> {
                    int themeMode;
                    switch (which) {
                        case 0: // 浅色
                            themeMode = AppCompatDelegate.MODE_NIGHT_NO;
                            break;
                        case 1: // 深色
                            themeMode = AppCompatDelegate.MODE_NIGHT_YES;
                            break;
                        default: // 跟随系统
                            themeMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
                            break;
                    }
                    ThemeManager.getInstance(requireContext()).setThemeMode(themeMode);
                    dialog.dismiss();
                })
                .show();
    }

    private void showLogoutDialog() {
        new MaterialAlertDialogBuilder(requireContext())
                .setTitle("退出登录")
                .setMessage("确定要退出登录吗？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", (dialog, which) -> {
                    UserManager.getInstance(requireContext()).logout();
                    startActivity(new Intent(requireContext(), LoginActivity.class));
                    requireActivity().finish();
                    requireActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                })
                .show();
    }
} 